package com.cskaoyan.service;

import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.domain.Comment;
import com.cskaoyan.bean.domain.CommentExample;
import com.cskaoyan.bean.goods.BasePageData;
import com.cskaoyan.mapper.CommentMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongProject: mall35th
 * @BelongPackage: com.cskaoyan.service
 * @Author: zijin
 * @Createtime: 2021-12-01 星期三 11:33
 * @Description: todo
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Override
    public BasePageData<Comment> queryCommentsByCondition(Integer userId, Integer valueId, BaseParam pageParams) {
        PageHelper.startPage(pageParams.getPage(),pageParams.getLimit());
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }
        criteria.andDeletedEqualTo(false);
        commentExample.setOrderByClause(pageParams.getSort() + " " + pageParams.getOrder());
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        BasePageData goodsList = BasePageData.commentOk(comments,pageParams);
        return goodsList;
    }

    @Override
    public void replyComment(Integer commentId, String content) {
        Comment comment = new Comment();
        comment.setAdminContent(content);
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andIdEqualTo(commentId);
        commentMapper.updateByExampleSelective(comment,commentExample);
    }

    /**
     * 逻辑删除评论
     * @param comment
     */
    @Override
    public void deleteComment(Comment comment) {
        comment.setDeleted(true);
        commentMapper.updateByPrimaryKey(comment);
    }
}
